package org.origin.centre.support.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;

import java.util.UUID;

/**
 * 日志追踪工具类
 *
 * @author ferret
 * @version 2024-05-08
 */
@SuppressWarnings("unused")
public final class TraceUtil {
    public final static String RequestNoKey = "requestNo";
    private final static Logger log = LoggerFactory.getLogger(TraceUtil.class);

    /**
     * 添加日志追踪交易流水号
     */
    public static void addTrace() {
        addTrace(createRequestNo());
    }

    /**
     * 添加日志追踪交易流水号
     */
    public static void addTrace(String requestNo) {
        MDC.put(RequestNoKey, requestNo);
        log.info("日志追踪交易流水号：{}", requestNo);
    }

    /**
     * 删除日志追踪交易流水号
     */
    public static void delTrace() {
        MDC.remove(RequestNoKey);
    }

    /**
     * 获取随机生成流水号
     */
    public static String getRequestNo() {
        return MDC.get(RequestNoKey);
    }

    /**
     * 创建随机生成流水号
     */
    public static String createRequestNo() {
        return UUID.randomUUID().toString().replaceAll("-", "");
    }

}
